<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;

/**use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Core\OssUtil;

 * Class Cpeizhi
 * @package App
 */
class Cpeizhi extends Model
{
    public $timestamps = false;
    protected $table = 'kl_punch_match_config';

    //是否可以回复
    const DEL_ON = 0;
    const DEL_OFF = 1;
	static $DelDisplay = [
        self::DEL_ON => '已删除',
        self::DEL_OFF => '未删除',
    ];
	
	//赛事状态
    const STATUS_YES = 1;
    const STATUS_NO = 0;
    static $StatusDisplay = [
        self::STATUS_NO => '关闭',
		self::STATUS_YES => '开启',
    ];
       /**
     * 删除
     * @param $c_id
     * @param $request
     * @return array
     * @throws \Exception
     */
    static function Dels($id, $request)
    {
        DB::beginTransaction();
		
        try {
			if (!DB::table('kl_punch_match_config')->where('id', $id)->update(['is_del' => 0])) {
				DB::rollBack();
                return ['message' => '删除失败', 'status' => 2];
            }
			
		} catch (Exception $exception) {
			return ['message' => '删除失败', 'status' => 2];
		}
		
        DB::commit();
 
        return ['status' => 1, 'message' => '删除成功'];
    }
	
    /**
     * 添加配置
     * @param $request
     * @return array
     */
    static function peizhiPublish($request)
    {
		$peizhi = new Cpeizhi();
        $peizhi->name = $request->name;
		$peizhi->day_num = $request->day_num;
		$peizhi->is_del = $request->is_del;
		$peizhi->gold_num = $request->gold_num;
		//$peizhi->status = $request->status;

        if ($request->has('pic_path')) {
			
            $path = Storage::url(Postings::OssUpdate($request->pic_path));
			echo $path; 
			exit();
            $peizhi->pic_path = $path;
        } else {
            return ['status' => 2, 'message' => '请上传图片'];
        }

        $peizhi->create_time = date('Y-m-d H:i:s', time());
        
        if (!$peizhi->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];

    }
	
	/*修改配置*/
    static function peizhiModify($request, $peizhi)
    {
		
        $peizhi->name = $request->name;
		$peizhi->day_num = $request->day_num;
		$peizhi->is_del = $request->is_del;
		$peizhi->gold_num = $request->gold_num;
		//$peizhi->status = $request->status;
        if ($request->has('pic_path')) {
            $path = Storage::url(Postings::OssUpdate($request->pic_path));
            $peizhi->pic_path = $path;
        } 
		
        if (!$peizhi->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];

    }


}
